package com.steven.treatment_nurse.activity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMCallStateChangeListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.EMNoActiveCallException;
import com.hyphenate.media.EMCallSurfaceView;
import com.steven.treatment_nurse.R;
import com.steven.treatment_nurse.base.BaseActivity;

public class VideoActivity extends BaseActivity implements View.OnClickListener {
    private String TAG = "video";
    private EMCallSurfaceView surfaceViewLocal;//本地视频流
    private EMCallSurfaceView surfaceViewRemote;//远端视频流
    private TextView tv_name;//呼叫方
    private ImageView ivReceive;//接收
    private ImageView ivRefuse;//拒绝
    private boolean isComingIn = false;//是否是呼叫进来的
    private EMCallStateChangeListener callStateChangeListener;

    @Override
    public int intiLayout() {
        return R.layout.activity_video;
    }

    @Override
    public void initView() {
        tv_name = findViewById(R.id.tv_name);//呼叫方名字
        surfaceViewLocal = (EMCallSurfaceView) findViewById(R.id.localSurface);
        surfaceViewRemote = (EMCallSurfaceView) findViewById(R.id.remoteSurface);
        ivReceive = findViewById(R.id.iv_receive);
        ivRefuse = findViewById(R.id.iv_refuse);
        ivReceive.setOnClickListener(this);
        ivRefuse.setOnClickListener(this);
        setListener();
        if (isComingIn) {
            //被呼叫

        } else {
            //主动呼叫对方

        }
        tv_name.setText(getIntent().getStringExtra("name"));
        EMClient.getInstance().callManager().setSurfaceView(surfaceViewLocal, surfaceViewRemote);
    }

    //注册视频呼叫监听
    private void setListener() {
        callStateChangeListener=new EMCallStateChangeListener() {
            @Override
            public void onCallStateChanged(CallState callState, CallError error) {
                switch (callState) {
                    case CONNECTING: // 正在连接对方
                        Log.e(TAG, "onCallStateChanged: 正在连接对方!");
                        break;
                    case CONNECTED: // 双方已经建立连接
                        Log.e(TAG, "onCallStateChanged: 双方已经建立连接");
                        break;

                    case ACCEPTED: // 电话接通成功
                        Log.e(TAG, "onCallStateChanged: 电话接通成功!");
                        break;
                    case DISCONNECTED: // 电话断了
                        if (error == CallError.ERROR_UNAVAILABLE) {
                            // 对方不在线
                            Log.e(TAG, "onCallStateChanged: 对方不在线!");
                        }
                        break;
                    case NETWORK_UNSTABLE: //网络不稳定
                        if (error == CallError.ERROR_NO_DATA) {
                            //无通话数据
                            Log.e(TAG, "onCallStateChanged: 无通话数据!");
                        } else {
                        }
                        break;
                    case NETWORK_NORMAL: //网络恢复正常
                        Log.e(TAG, "onCallStateChanged: 网络恢复正常!");
                        break;
                    case NETWORK_DISCONNECTED: //通话中对方断网会执行
                        Log.e(TAG, "onCallStateChanged: 通话中对方网络终端!");
                        break;
                    default:
                        break;
                }

            }
        };
        EMClient.getInstance().callManager().addCallStateChangeListener(callStateChangeListener);
    }


    //接听通话
    private void receiveCall() {
        try {
            EMClient.getInstance().callManager().answerCall();
        } catch (EMNoActiveCallException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //拒绝通话
    private void refuseCall() {
        try {
            EMClient.getInstance().callManager().rejectCall();
        } catch (EMNoActiveCallException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        surfaceViewLocal.getRenderer().dispose();
        surfaceViewLocal = null;
        surfaceViewRemote.getRenderer().dispose();
        surfaceViewRemote = null;
        if(callStateChangeListener != null)
            EMClient.getInstance().callManager().removeCallStateChangeListener(callStateChangeListener);
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_receive://接受呼叫
                receiveCall();
                break;
            case R.id.iv_refuse://拒绝呼叫
                refuseCall();
                finish();
                break;
        }
    }
}
